package bookshop.controller.backend;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Manager;
import bookshop.service.ManagerService;
import bookshop.service.helper.ManagerQueryHelper;
import bookshop.service.impl.ManagerServiceImpl;
import bookshop.utils.Page;

public class ManagerServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		
		
		
         request.setCharacterEncoding("utf-8");
         String act =request.getParameter("act");
         
         
         if("input_manager".equals(act)){
        	 request.getRequestDispatcher("jsps/backend/manager/input_manager.jsp").forward(request, response);
         }
         else if ("create".equals(act)) {
 			
 			String  manager_id=request.getParameter("managerid");
 			String  manager_pwd=request.getParameter("managerpwd");
 			String  manager_name=request.getParameter("managername");
 			
 			Manager manager=new Manager();
        	manager.setLoginId(manager_id);
 			manager.setLoginPwd(manager_pwd);
 			manager.setRealName(manager_name);
 			
 			
 			
 		 ManagerService managerService=new ManagerServiceImpl();
 		 managerService.createManager(manager);
 		 response.sendRedirect("managerMgr?act=loadManagers");
 						
 		}
         else if("loadall".equals(act)){
 			
 			ManagerService mgService=new ManagerServiceImpl();
 			request.setAttribute("managers", mgService.loadall());
 			request.getRequestDispatcher("jsps/backend/manager/list_manager.jsp").forward(request, response);
 		}
         else if("loadManagers".equals(act)){
        	 
//        	 设置一个查询存储参数容器
             ManagerQueryHelper helper=new ManagerQueryHelper();
//            根据情况，把提交过来的查询参数写入helper，以方便传给后台，进行记录的条件过滤。
             if(request.getParameter("qryManagerId")!=null && request.getParameter("qryManagerId").length()!=0)
            	  helper.setQryManagerId(request.getParameter("qryManagerId"));
             if(request.getParameter("qryManagerName")!=null && request.getParameter("qryManagerName").length()!=0)
           	  helper.setQryManagerName(request.getParameter("qryManagerName"));
             
//          创建页面对象，如果外界没有指定要第几页，默认就是第一页，如果有指定。比如managerMgr?act=loadManagers%pageNo=2,就是第2页
             Page page=new Page();
             if(request.getParameter("pageNo")!=null)
            	 page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
             
             ManagerService managerService=new ManagerServiceImpl();
//           helper未指定，也就是说，所有的属性为null,则分页就是对所有记录的分页。
//           如果helper有指定属性，则分页，是基于组合查询基础上的分页。  
   			 request.setAttribute("page", managerService.loadManagerByCondition(helper,page));
   			 request.getRequestDispatcher("jsps/backend/manager/list_manager.jsp").forward(request, response);	 
        	 
         }
         else if ("update".equals(act)) {
 			
 			
 			String  manager_id=request.getParameter("login_id");
 			String  manager_pwd=request.getParameter("login_pwd");
 			String  manager_name=request.getParameter("realname");
 			
 			
 			ManagerService managerService=new ManagerServiceImpl();
 			Manager manager=managerService.getManagerById(manager_id);
 			manager.setLoginPwd(manager_pwd);
 			manager.setRealName(manager_name);
 			managerService.updateManager(manager);
 			
 			response.sendRedirect("managerMgr?act=loadManagers");
 									
 		}
 		else if("dele".equals(act)){
 			
 			String manager_id=request.getParameter("loginId");
 			ManagerService managerService=new ManagerServiceImpl();
 			managerService.deleteManager(manager_id);
 			response.sendRedirect("managerMgr?act=loadManagers");			
 		}
 		else if("get_id".equals(act)){
 			
 			String  manager_id=request.getParameter("loginId");
 			ManagerService managerService=new ManagerServiceImpl();
 			Manager manager=managerService.getManagerById(manager_id);
 			request.setAttribute("manager", manager);
 			request.getRequestDispatcher("jsps/backend/manager/update_manager.jsp").forward(request, response);									
 		}
 		else if("freeze".equals(act)){
 			
 			String  manager_id=request.getParameter("loginId");
 			String  manager_status="N";
 			ManagerService managerService=new ManagerServiceImpl();
 			Manager manager=managerService.getManagerById(manager_id);
 			manager.setStatus(manager_status);
 			managerService.up_status(manager);
 			response.sendRedirect("managerMgr?act=loadManagers");
 		}
        else if("defreeze".equals(act)){
 			
 			String  manager_id=request.getParameter("loginId");
 			String  manager_status="Y";
 			ManagerService managerService=new ManagerServiceImpl();
 			Manager manager=managerService.getManagerById(manager_id);
 			manager.setStatus(manager_status);
 			managerService.up_status(manager);
 			response.sendRedirect("managerMgr?act=loadManagers");
 		}
 		
 		

 		
 	}
		
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request,response);
	}

}




